import { usePageRequest, useRequest } from "../../../utils/request";

Component({
    properties: {},
    data: {
        data: [],
        status: 0,
        statusText: {
            1: '空置中',
            2: '待上架',
            3: '审核中...',
            4: '待释放',
            5: '已出租',
            6: '删除',
            7: '审核失败'
        },
        barStatusText: {
            0: '运营中',
            1: '待上架',
            2: '审核'
        }
    },
    methods: {
        onLoad() {
            this.normalRequest = new usePageRequest('api/onstatushouse', {}, this)
            this.downRequest = new usePageRequest('api/onwaithouse', {
                hold: true
            }, this)
            this.reviewRequest = new usePageRequest('api/oncheckhouse', {
                hold: true
            }, this)

            // 上架
            this.upHouseRequest = new useRequest('api/owneruphouse', { hold: true }, this)

            // 下架
            this.downHouseRequest = new useRequest('api/ownerdownhouse', { hold: true }, this)

            // 删除
            this.removeRequest = new useRequest('api/ownerdelhouse', { hold: true }, this)

            // 释放
            this.fanRequest = new useRequest('api/ownerreleasehouse', { hold: true }, this)
        },
        onPullDownRefresh() {
            switch (this.data.status) {
                case 0:
                    this.normalRequest.reset().then(() => {
                        wx.stopPullDownRefresh()
                    })
                    break;
                case 1:
                    this.downRequest.reset().then(() => {
                        wx.stopPullDownRefresh()
                    })
                    break;
                case 2:
                    this.reviewRequest.reset().then(() => {
                        wx.stopPullDownRefresh()
                    })
                    break;
            }
        },
        changeStatus(e) {
            const index = Number(e.detail.index)
            this.setData({
                status: index
            })
            wx.startPullDownRefresh()
        },
        onReachBottom() {
            switch (this.data.status) {
                case 0:
                    this.normalRequest.next()
                    break;
                case 1:
                    this.downRequest.next()
                    break;
                case 2:
                    this.reviewRequest.next()
                    break;
            }
        },
        // 上架
        up(e) {
            wx.showModal({
                title: '确认上架该房源',
                content: '上架后可对外出租',
                success: result => {
                    if (result.confirm) {
                        const index = e.currentTarget.dataset.index
                        this.upHouseRequest.run({
                            house_id: this.data.data[index].house_id
                        }).then(() => {
                            this.changeStatus({ detail: { index: 0 } })
                        })
                    }
                }
            })
        },
        // 下架
        down(e) {
            wx.showModal({
                title: '确定下架该房源',
                content: '下架之后不可再对外出租了',
                success: result => {
                    if (result.confirm) {
                        const index = e.currentTarget.dataset.index
                        this.downHouseRequest.run({
                            house_id: this.data.data[index].house_id
                        }).then(() => {
                            this.changeStatus({ detail: { index: 1 } })
                        })
                    }
                }
            })
        },
        // 释放
        fan(e) {
            wx.showModal({
                title: '确定释放该房源',
                content: '释放之后可对外出租',
                success: result => {
                    if (result.confirm) {
                        const index = e.currentTarget.dataset.index
                        this.fanRequest.run({
                            house_id: this.data.data[index].house_id
                        }).then(() => {
                            this.changeStatus({ detail: { index: 0 } })
                        })
                    }
                }
            })
        },
        // 编辑
        edit(e) {
            const index = e.currentTarget.dataset.index
            wx.navigateTo({
                url: '/pages/house/edit/index/index?id=' + this.data.data[index].house_id
            })
        },
        lock(e){
            const index = e.currentTarget.dataset.index
            wx.navigateTo({
                url: '/pages/house/lock/lock?id=' + this.data.data[index].house_id
            })
        },
        // 删除
        remove(e) {
            wx.showModal({
                title: '确认删除该房源',
                content: '确认后房源将永久删除',
                success: result => {
                    if (result.confirm) {
                        const index = e.currentTarget.dataset.index
                        this.removeRequest.run({
                            house_id: this.data.data[index].house_id
                        }).then(() => {
                            const data = this.data.data
                            data.splice(index, 1)
                            this.setData({
                                data
                            })
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success'
                            })
                        })
                    }
                }
            })
        },
    }
});
